package com.right.health.app.ui.medical;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.bean.MarriageResponse;
import com.right.health.app.net.SickHistoryApi;

/*
 * Created by hopeliao on 2019-09-30
 * 婚育史
 *
 */
public class MarriageHistoryActivity extends BaseActivity implements View.OnClickListener, OnHttpListener {

    private static final int HTTP_CODE_UPDATE = 1;

    private CheckBox[] mCheckBoxGroup1 = new CheckBox[2];
    private TextView[] mTipsGroup1 = new TextView[2];

    private CheckBox[] mCheckBoxGroup2 = new CheckBox[4];
    private TextView[] mTipsGroup2 = new TextView[4];

    private SickHistoryApi mSickHistoryApi;

    private int mGroupSelectedIndex1 = -1, mGroupSelectedIndex2 = -1;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.marriage_history);
    }


    @Override
    public void findView() {
        mCheckBoxGroup1[0] = (CheckBox) findViewById(R.id.cb_selected1);
        mCheckBoxGroup1[1] = (CheckBox) findViewById(R.id.cb_selected2);

        mTipsGroup1[0] = (TextView) findViewById(R.id.tv_tips1);
        mTipsGroup1[1] = (TextView) findViewById(R.id.tv_tips2);

        for (CheckBox cb : mCheckBoxGroup1) {
            cb.setOnClickListener(groupClick1);
        }

        mCheckBoxGroup2[0] = (CheckBox) findViewById(R.id.cb_selected3);
        mCheckBoxGroup2[1] = (CheckBox) findViewById(R.id.cb_selected4);
        mCheckBoxGroup2[2] = (CheckBox) findViewById(R.id.cb_selected5);
        mCheckBoxGroup2[3] = (CheckBox) findViewById(R.id.cb_selected6);

        for (CheckBox cb : mCheckBoxGroup2) {
            cb.setOnClickListener(groupClick2);
        }

        mTipsGroup2[0] = (TextView) findViewById(R.id.tv_tips3);
        mTipsGroup2[1] = (TextView) findViewById(R.id.tv_tips4);
        mTipsGroup2[2] = (TextView) findViewById(R.id.tv_tips5);
        mTipsGroup2[3] = (TextView) findViewById(R.id.tv_tips6);

        findViewById(R.id.btn_save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send2Net();
            }
        });

    }

    private void send2Net() {
        if (mGroupSelectedIndex1 == -1) {
            ToastUtil.show(this, "请选择您的婚姻状态");
            return;
        }
        if (mGroupSelectedIndex2 == -1) {
            ToastUtil.show(this, "请选择您的生育状况");
            return;
        }

        mSickHistoryApi.addRequestCode(HTTP_CODE_UPDATE);
        mSickHistoryApi.updateMarriage(App.getInstance().getCurrentMember().id, String.valueOf(mGroupSelectedIndex1), String.valueOf(mGroupSelectedIndex2));
    }

    private View.OnClickListener groupClick1 = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            for (int i = 0; i < mCheckBoxGroup1.length; i++) {
                if (v.getId() == mCheckBoxGroup1[i].getId()) {
                    if (mCheckBoxGroup1[i].isChecked()) {
                        mGroupSelectedIndex1 = i;
                        mTipsGroup1[i].setTextColor(getResources().getColor(R.color.color_09C3BE));
                    } else {
                        mTipsGroup1[i].setTextColor(getResources().getColor(R.color.color_777777));
                        mGroupSelectedIndex1 = -1;
                    }
                } else {
                    mCheckBoxGroup1[i].setChecked(false);
                    mTipsGroup1[i].setTextColor(getResources().getColor(R.color.color_777777));
                }
            }
        }
    };

    private View.OnClickListener groupClick2 = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            for (int i = 0; i < mCheckBoxGroup2.length; i++) {
                if (v.getId() == mCheckBoxGroup2[i].getId()) {
                    if (mCheckBoxGroup2[i].isChecked()) {
                        mGroupSelectedIndex2 = i;
                        mTipsGroup2[i].setTextColor(getResources().getColor(R.color.color_09C3BE));
                    } else {
                        mGroupSelectedIndex2 = -1;
                        mTipsGroup2[i].setTextColor(getResources().getColor(R.color.color_777777));
                    }
                } else {
                    mCheckBoxGroup2[i].setChecked(false);
                    mTipsGroup2[i].setTextColor(getResources().getColor(R.color.color_777777));
                }
            }
        }
    };

    @Override
    public void initialize() {
        mSickHistoryApi = new SickHistoryApi();
        mSickHistoryApi.setListener(this);

        mSickHistoryApi.getMarriageHistory(App.getInstance().getCurrentMember().id);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onResponse(Request request, Response response) {
        if (response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            if (request.requestCode == HTTP_CODE_UPDATE) {
                ToastUtil.show(this, "修改成功");
                finish();
            } else {
                if (response.targetData instanceof MarriageResponse) {
                    MarriageResponse marriageResponse = response.cast(MarriageResponse.class);
                    try {
                        mCheckBoxGroup1[Integer.valueOf(marriageResponse.maritalreproductivehistory.maritaltype)].performClick();
                        mCheckBoxGroup2[Integer.valueOf(marriageResponse.maritalreproductivehistory.reproductivetype)].performClick();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
